from db.metastore import connect_with_pocketbase, user_auth_via_client, client_create_new, client_delete_record, \
    client_fetch_from, client_fetch_full_list_from, client_update_to
from application.handle_exception import pb_operation_exception
from domain.artifact import ArtifactInDB, Artifact
from domain.authentication import User


async def add_new_artifact_record(
        new_record: ArtifactInDB,
        user: User
):
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        create_response = await client_create_new(
            client=db_client,
            collection="artifacts",
            data=new_record.dict()
        )
        if not create_response:
            raise pb_operation_exception
    return create_response


async def view_artifact_information(
        target_record_id: str,
        user: User
):
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        if target_record_id is None:
            record_response = await client_fetch_full_list_from(
                client=db_client,
                collection="artifacts"
            )
        else:
            record_response = await client_fetch_from(
                client=db_client,
                collection="artifacts",
                target_id=target_record_id
            )

    return record_response


async def update_artifact_metadata_record(
        user: User,
        update_record_id: str,
        new_metadata: Artifact,
):
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        record_response = await client_fetch_from(
            client=db_client,
            collection="artifacts",
            target_id=update_record_id
        )
        record_dict = record_response.__dict__
        new_artifact_record = ArtifactInDB(
            id=record_dict['id'],
            uuid=user.uuid.hex,
            name=record_dict['name'],
            artifact_info=new_metadata.json()
        )
        update_record_response = await client_update_to(
            client=db_client,
            collection="artifacts",
            target_id=update_record_id,
            data=new_artifact_record.dict()
        )
        if not update_record_response:
            raise pb_operation_exception
    return update_record_response


async def delete_artifact_record(
        target_artifact_id: str,
        user: User
):
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        null_content_response = await client_delete_record(
            client=db_client,
            collection="artifacts",
            target_id=target_artifact_id
        )
        if not null_content_response:
            raise pb_operation_exception
    return null_content_response
